package cn.dgut.training.service.impl;

import cn.dgut.training.dto.AdminDTO;
import cn.dgut.training.entity.Admin;
import cn.dgut.training.mapper.AdminMapper;
import cn.dgut.training.service.IAdminService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.pageVO.PageVO;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private AdminMapper adminMapper;

    @Autowired
    public BaseVO listUserByPage(AdminDTO adminDTO) {

        //构造分页
        IPage page = new Page();
        BeanUtils.copyProperties(adminDTO, page);
        //获取条件查询构造器
        QueryWrapper queryWrapper = adminDTO.getWrapper(adminDTO);

        IPage res = adminMapper.selectPage(page, queryWrapper);

        BaseVO baseVO = new BaseVO(res);

        return baseVO;
    }


    public List<Admin> findAdmin(String username) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(!StringUtils.isBlank(username))
        queryWrapper.eq("username",username);

        List<Admin> admins= null;
            admins = adminMapper.selectList(queryWrapper);

        return admins;
    }

    public List<Admin> all() {

        QueryWrapper queryWrapper=new QueryWrapper();

        List<Admin> admins=adminMapper.selectList(queryWrapper);

        return admins;
    }

    public PageVO querySelective(String name) {

        AdminDTO adminDTO=new AdminDTO();
        adminDTO.setUsername(name);
        //构造分页
        IPage page1 = new Page();
        BeanUtils.copyProperties(adminDTO, page1);
        //获取条件查询构造器
        QueryWrapper queryWrapper = adminDTO.getWrapper(adminDTO);

        List<Admin> admins= adminMapper.selectList( queryWrapper);
        List<AdminDTO> adminDTOS=new ArrayList<>();
        for(Admin admin:admins){
                AdminDTO adminDTO1=new AdminDTO();
                adminDTO1.setId(admin.getId());
                adminDTO1.setUsername(admin.getUsername());
                adminDTO1.setPassword(admin.getPassword());
                adminDTO1.setAvatar(admin.getAvatar());
                adminDTO1.setRoleIds(admin.getArrRoleIds());
                adminDTOS.add(adminDTO1);
        }


        PageVO pageVO = new PageVO("default",adminDTOS);

        return pageVO;

    }

    public void add(Admin admin) {
        admin.setAddTime(DateTime.now());
        admin.setUpdateTime(DateTime.now());
        adminMapper.insert(admin);

    }
}
